﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OAuth2.Framework;
using System.Collections.Specialized;
using OAuth2.Framework.Utility;

namespace OAuth2.Client
{
    //Authorization Code
    public class AuthorizationRequest
    {
        /// <summary>
        /// REQUIRED.Value MUST be set to code
        /// </summary>
        public string ResponseType { get; set; }

        /// <summary>
        /// REQUIRED.
        /// </summary>
        public IClient Client { get; set; }

        /// <summary>
        /// OPTION.unless a redirection URI has been established between the client and
        /// authorization server via other means.
        /// </summary>
        public Uri RedirectUri { get; set; }

        /// <summary>
        /// OPTIONAL. The scope of the access request expressed as a list of space-delimited,
        /// case sensitive strings. The value is defined by the authorization server. If the value
        /// contains multiple space-delimited strings, their order does not matter, and each
        /// string adds an additional access range to the requested scope.
        /// </summary>
        public string[] Scope { get; set; }

        /// <summary>
        /// An opaque value used by the client to maintain state between the
        /// request and callback. The authorization server includes this value when redirecting
        /// the user-agent back to the client.
        /// </summary>
        public string state { get; set; }

        public Uri ProviderEndpoint { get; set; }

        public string Method { get; set; }

        public string ToAbsoluteUri() 
        {
            UriBuilder builder = new UriBuilder(ProviderEndpoint);
            NameValueCollection components = new NameValueCollection();
            components[Parameters.ResponseType] = ResponseType;
            components[Parameters.ClientId] = Client.ClientID;
            if (Scope != null && Scope.Length > 0)
            {
                components[Parameters.Scope] = string.Join(" ",Scope);
            }

            components[Parameters.RedirectUri] = RedirectUri.AbsoluteUri;
            builder.Query = UriHelper.ReconstructQueryString(components);

            return builder.Uri.AbsoluteUri;
        }
    }
}
